package com.joker.aigc.chat.controller;


import com.joker.aigc.chat.service.impl.SocketServiceImpl;
import com.joker.aigc.domain.entity.R;
import io.netty.channel.Channel;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author : feixiang.li
 * @since : 2025-03-30 15:50
 */
@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("/api/1.0/online")
@Tag(name = "online", description = "获取所有在线用户")
public class GetAllOnlineUserController {

    @Autowired
    SocketServiceImpl socketService;


    @RequestMapping("/getAllOnlineUsers")
    public R<List<String>> getAllOnlineUsers() {
        ConcurrentHashMap<Long, CopyOnWriteArrayList<Channel>> data = socketService.ONLINE_UID_MAP;
        List<String> onlineUsers = new CopyOnWriteArrayList<>();
        for (Long id : data.keySet()) {
            log.info("id: {}", id);
            onlineUsers.add(String.valueOf(id));
        }
        return R.ok(onlineUsers);
    }
}
